package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.list.SearchFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import com.atguigu.gmall.web.bean.PropBread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/18 16:45
 */
@Controller
public class ListController {
    @Autowired
    private SearchFeignClient searchFeignClient;
//   @GetMapping("/list.html")
//    public String searchPage(@RequestParam(value = "category3Id",required = false) Long category3Id,
//                             @RequestParam(value = "keyword",required = false) String keyword,
//                             @RequestParam(value = "trademark",required = false) String trademark,
//                             @RequestParam(value = "props",required = false) String[] props,
//                             @RequestParam(value = "order",required = false) String order,
//                             @RequestParam(value = "pageNo",required = false) Long pageNo){
//        return "list/index";
//    }

    @GetMapping("/list.html")
    public String searchPage(SearchParam param, Model model){//获取请求参数中的值，封装成JavaBean
        // 根据指定的查询条件，远程调用ES查询
        Map<String,Object> search = searchFeignClient.search(param);
        model.addAllAttributes(search);
        model.addAttribute("searchParam",param);//检索参数的回显
        //urlParam: 保存以前的访问路径，并且根据这次检索动态拼接出url的真正内容
        String url = makeUrlParam(param);
        model.addAttribute("urlParam",url);

        HashMap<Object,Object> map = new HashMap<>();
        if(!StringUtils.isEmpty(param.getOrder())){
            map.put("type",param.getOrder().split(":")[0]);
            map.put("sort",param.getOrder().split(":")[1]);
            model.addAttribute("orderMap",map);
        }else{
            map.put("type",1);
            map.put("sort","desc");
            model.addAttribute("orderMap",map);
        }
        //构建品牌的面包屑 trademarkParam
        String trademarkParam = markTrademarkParam(param);
        model.addAttribute("trademarkParam",trademarkParam);
        //平台属性的面包屑 propsParamList
        List<PropBread> propsParamList = markPropsParamList(param);
        model.addAttribute("propsParamList",propsParamList);
        return "list/index";
    }
    //平台属性的面包屑 propsParamList
    private List<PropBread> markPropsParamList(SearchParam param) {
        List<PropBread> propsParamList = new ArrayList<>();
        if(param.getProps()!=null && param.getProps().length > 0){
           for (String prop : param.getProps()) {
               String[] split = prop.split(":");

               PropBread propBread = new PropBread(Long.parseLong(split[0]),split[2],split[1]);
               propsParamList.add(propBread);
           }
           return propsParamList;
       }
        return null;
    }

    //构建品牌的面包屑
    private String markTrademarkParam(SearchParam param) {
        if(!StringUtils.isEmpty(param.getTrademark())){
            String trademark = param.getTrademark();
            return "品牌: "+param.getTrademark().split(":")[1];
        }
        return null;
    }

    private String makeUrlParam(SearchParam param) {
        StringBuilder base = new StringBuilder("list.html?");
        if(!StringUtils.isEmpty(param.getTrademark())){
            //比如   trademark=1:小米
            base.append("&trademark="+param.getTrademark());
        }
        if(param.getCategory3Id()!=null){
            base.append("&category3Id="+param.getCategory3Id());
        }

        if(!StringUtils.isEmpty(param.getKeyword())){
            base.append("&keyword="+param.getKeyword());
        }

//        if(param.getPageNo()!=null){
//            base.append("&pageNo="+param.getPageNo());
//        }
//        if(param.getPageSize()!=null){
//            base.append("&pageSize="+param.getPageSize());
//        }
        if(param.getProps()!=null && param.getProps().length > 0){
            for (String prop : param.getProps()) {
                base.append("&props="+prop);
            }
        }
//        if(!StringUtils.isEmpty(param.getOrder())){
//            base.append("&order="+param.getOrder());
//        }
        return base.toString();
    }
}

